<template>
  <el-container class="default">
    <el-header class="header-box">
      <div class="menu-nav">
        <!-- logo图片 -->
        <img src="../static/logo.png" class="logo" alt="logo">
        <div class="menu-list">
          <nuxt-link to="/" exact-active-class='active' class="menu-item" >首页</nuxt-link>
          <nuxt-link to="/tag" exact-active-class='active' class="menu-item" >标签</nuxt-link>
          <nuxt-link to="/sort" exact-active-class='active' class="menu-item" >排行</nuxt-link>
          <nuxt-link to="/about" exact-active-class='active' class="menu-item" >关于</nuxt-link>
        </div>
        <div class="user-nav">
          <el-dropdown v-show="status">
            <div class="info">
              <span class="username">{{ username }}</span>
              <img :src="avatar" class="avatar">
              <i class="el-icon-caret-bottom down-icon" />
            </div>
            <el-dropdown-menu slot="dropdown" class="link-btn">
              <el-dropdown-item @click.native="$router.push('/admin')">后台中心</el-dropdown-item>
              <el-dropdown-item @click.native="$router.push('/admin/user')">个人中心</el-dropdown-item>
              <el-dropdown-item :disabled="buttonLoad" divided @click.native="logout(true)">切换用户</el-dropdown-item>
              <el-dropdown-item :disabled="buttonLoad" @click.native="logout(false)">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <div v-show="!status">
            <span @click="toSignPageHandle('0')" class="btn">登录</span>
            <span @click="toSignPageHandle('1')" class="btn">注册</span>
          </div>
        </div>
      </div>
    </el-header>

    <el-main class="main-box" style="background-color: rgba(241,243,244, 0.3)">

      <Nuxt />

      <el-backtop :right="50" :bottom="60">
        <div class="back-top">
          <svg-icon icon-class="up-arrow" class="fz20" />
        </div>
      </el-backtop>
    </el-main>

    <el-footer class="footer-box">
      2021 版权所有：吴有威
    </el-footer>
  </el-container>
</template>

<script>
  /*
    默认模板
   */
  export default {
    name: 'default',
    data() {
      return {
        // 按钮是否可被点击[false-可以被点击 true-不可被点击]
        buttonLoad: false,
      }
    },
    computed: {
      // 用户名称
      username() {
        return this.$store.state.user.username
      },
      // 用户头像
      avatar() {
        return this.$store.state.user.avatar
      },
      // 用户登录状态
      status() {
        return this.$store.state.user.status
      }
    },
    methods: {
      /**
       * 跳转到用户登录/注册页面
       * @param {string} optType 操作类型[0-登录 1-注册]
       */
      toSignPageHandle(optType) {
        this.$router.push({ name: 'sign', params: { ...this.$route.params, 'optType': optType, 'prePage': this.$route.name }})
      },
      /**
       * 用户退出登录方法
       * @param {boolean} bool 退出登录方式（true-切换用户 false-直接退出）
       */
      logout(bool) {
        // 按钮不可被点击
        this.buttonLoad = true

        this.$axios.$get('/sys/user/logout')
          .then(() => {
            this.$notify({
              title: '退出登录成功',
              type: 'success',
              message: this.username + "登录退出成功",
              offset: 100,
              duration: 1500
            });

            this.$store.commit('user/setUserId', '')
            this.$store.commit('user/setUsername', '')
            // this.$store.commit('user/setAvatar', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80')
            this.$store.commit('user/setAvatar', '')
            this.$store.commit('user/setStatus', false)

            // 从cookie中删除用户信息
            this.$cookies.remove('token')
            this.$cookies.remove('userId')
            this.$cookies.remove('username')
            this.$cookies.remove('avatar')

            if (bool) {
              this.toSignPageHandle("0")
            }
          })
          .catch(() => {})
          .finally(() => {
            // 按钮恢复可点击
            this.buttonLoad = false
          })
      }
    }
  }
</script>

<!-- 顶部导航栏 -->
<style scoped>
  .default {
    font-size: 15px;
    word-spacing: 1px;
    -ms-text-size-adjust: 100%;
    -webkit-text-size-adjust: 100%;
    -moz-osx-font-smoothing: grayscale;
    -webkit-font-smoothing: antialiased;
    box-sizing: border-box;
  }
  .header-box {
    height: 60px;
    line-height: 60px;
    width: 100%;
    background-color: #c4d7d6;
    box-shadow: 0 1px 3px 0 rgb(0 0 0 / 12%), 0 0 3px 0 rgb(0 0 0 / 4%);
  }
  .menu-nav {
    height: 100%;
    width: 74%;
    margin: 0 auto;
  }
  .menu-nav .logo {
    float: left;
    width: 150px;
    height: 100%;
  }
  .menu-nav .menu-list {
    float: left;
    margin-left: 30px;
    height: 100%;
    display: flex;
  }
  .menu-nav .menu-list .menu-item {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 20px;
    font-size: 16px;
    text-decoration: none;
    color: #333;
    text-align: center;
    position: relative;
    user-select: none;
  }
  .menu-nav .menu-list .menu-item:after {
    content: '';
    position: absolute;
    left: 50%;
    bottom: 0;
    width: 1px;
    height: 2px;
    background: #4A9946;
    transform: translateX(-150%);
    -webkit-transform: translateX(-150%);
  }
  .menu-nav .menu-list .menu-item:hover {
    color: #4A9946;

    text-decoration: none;
  }
  .menu-nav .menu-list .menu-item:hover::after {
    width: calc(100% - 50px);
    transition: transform .3s ease;
    transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
  }
  .menu-nav .menu-list .active {
    color: #4A9946;
  }
  .menu-nav .user-nav {
    height: 100%;
    float: right;
  }
  .menu-nav .user-nav .info {
    height: 100%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .menu-nav .user-nav .info .username {
    font-size: 16px;
    font-family: SimHei;
    margin-right: 10px;
  }
  .menu-nav .user-nav .info .avatar {
    width: 45px;
    height: 45px;
    border-radius: 10px;
  }
  .menu-nav .user-nav .info .down-icon {
    margin-top: 15px;
    transition: margin-top .28s;
  }
  .menu-nav .user-nav .btn {
    font-size: 17px;
    cursor: pointer;
    color: #333;
  }
  .menu-nav .user-nav .btn:nth-child(1) {
    margin-right: 15px;
  }
  .menu-nav .user-nav .btn:hover {
    color: #1ba784;
  }
</style>

<!-- 底部样式 -->
<style scoped>
  .footer-box {
    height: 60px;
    line-height: 60px;
    width: 100%;
    background-color: #c4d7d6;
    text-align: center;
    box-shadow: 0 -3px 15px rgb(0 0 0 / 12%), 0 0 3px 0 rgb(0 0 0 / 4%);
  }
</style>

<style scoped>
  .back-top {
    height: 100%;
    width: 100%;
    background-color: #f2f5f6;
    box-shadow: 0 0 6px rgba(0,0,0, .12);
    text-align: center;
    line-height: 40px;
    color: #0f95b0;
  }
</style>

<!-- 穿透样式 -->
<style scoped>

/deep/ .el-header {
  padding: 0;
}
/deep/ .el-main {
  padding: 0;
}
/deep/ .el-footer {
  padding: 0;
}

/* 用户下拉菜单穿透样式 */
/*/deep/ .el-dropdown-menu__item {
  text-align: center;
}
/deep/ .el-dropdown-menu__item:hover {
  background-color: #c4d7d6;
  color: #1ba784;
}*/
</style>
